<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	var studentDatagrid;
	var studentCorporationDatagrid;
	var studentId;
	$(function() {
		studentDatagrid = $('#student_studentList_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/studentAction!queryStudent.action',
			fit : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20 ],
			sortName : 'studentName',
			sortOrder : 'asc',
			checkOnSelect : true,
			selectOnCheck : true,
			singleSelect : true,
			striped : true,
			frozenColumns : [ [ {
				field : 'id',
				title : '编号',
				width : 150,
				checkbox : true
			}, {
				field : 'studentName',
				title : '姓名',
				width : 80,
				align : 'left',
				sortable : true
			}, {
				field : 'studentNo',
				title : '学号',
				width : 80,
				align : 'left',
				sortable : true
			} ] ],
			columns : [ [ {
				field : 'gender',
				title : '性别',
				width : 50,
				hidden : true
			}, {
				field : 'genderName',
				title : '性别',
				width : 50,
				align : 'center'
			}, {
				field : 'title',
				title : '职称',
				width : 100,
				align : 'left'
			}, {
				field : 'corporationId',
				title : '工作单位主键',
				width : 80,
				hidden : true
			}, {
				field : 'corporationName',
				title : '工作单位',
				width : 150,
				align : 'left'
			}, {
				field : 'position',
				title : '职务',
				width : 100,
				align : 'left'
			}, {
				field : 'mobile',
				title : '手机',
				width : 100,
				align : 'left'
			}, {
				field : 'qq',
				title : 'QQ',
				width : 80,
				align : 'left'
			}, {
				field : 'email',
				title : '电子邮件',
				width : 150,
				align : 'left'
			}, {
				field : 'phone',
				title : '固话',
				width : 100,
				align : 'left'
			}, {
				field : 'fax',
				title : '传真',
				width : 100,
				align : 'left'
			}, {
				field : 'identityType',
				title : '证件类型',
				width : 100,
				hidden : true
			}, {
				field : 'politicalStatus',
				title : '政治面貌',
				width : 100,
				hidden : true
			}, {
				field : 'bloodType',
				title : '血型',
				width : 100,
				hidden : true
			} ] ],
			toolbar : [ {
				text : '查询',
				iconCls : 'icon-search',
				handler : function() {
					queryStudent();
				}
			}, '-', {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					addStudent();
				}
			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					editStudent();
				}
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					removeStudent();
				}
			}, '-', {
				text : '查看',
				iconCls : 'icon-view',
				handler : function() {
					viewStudent();
				}
			}, '-', {
				text : '导入',
				iconCls : 'icon-down',
				handler : function() {
					importStudent();
				}
			}, '-', {
				text : '模板下载',
				iconCls : 'icon-up',
				handler : function() {
					downloadTemplate();
				}
			} ]
		});

		$('.datagrid-header div').css({
			'textAlign' : 'center'
		});

		function queryStudent() {
			$('#student_studentList_queryForm input').val('');
			$('#student_studentList_queryDialog').dialog('open');
		}

		function addStudent() {
			$('#student_studentAdd_addForm input').val('');
			var dlg = $('<div style="padding:10px;" />').dialog({
				width : 520,
				height : 280,
				href : '${pageContext.request.contextPath}/pages/student/studentAdd.jsp',
				modal : true,
				title : '学生新增',
				buttons : [ {
					text : '保存',
					handler : function() {
						$('#student_studentAdd_addForm').form('submit', {
							url : '${pageContext.request.contextPath}/studentAction!addStudent.action',
							success : function(r) {
								var obj = jQuery.parseJSON(r);
								if (obj.success) {
									studentDatagrid.datagrid('insertRow', {
										index : 0,
										row : obj.obj
									});
									dlg.dialog('close');
									$.messager.show({
										title : '提示',
										msg : obj.msg
									});
								} else {
									$.messager.alert('提示', obj.msg);
								}
							}
						});
					}
				}, {
					text : '取消',
					handler : function() {
						$('#student_studentAdd_addForm input').val('');
						dlg.dialog('close');
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
				}
			});
		}

		function editStudent() {
			var rows = studentDatagrid.datagrid('getChecked');
			if (rows.length == 1) {
				studentId = rows[0].id;
				var dlg = $('<div />').dialog({
					width : 865,
					height : 412,
					href : '${pageContext.request.contextPath}/pages/student/studentEdit.jsp',
					modal : true,
					title : '学生编辑',
					onClose : function() {
						$(this).dialog('destroy');
					},
					onLoad : function() {
						// 基本信息
						$('#student_studentEdit_editGeneralForm').form('load', rows[0]);
						// 专业履历
						studentResumeDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 个人爱好
						$('#student_studentEdit_editHobbyForm').form('load', '${pageContext.request.contextPath}/hobbyAction!queryHobby.action?studentId=' + studentId);
						// 车辆信息
						studentVehicleDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 人际关系
						studentRelationshipDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 家庭信息
						studentFamilyDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 教育信息
						studentEducationDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 培训信息
						studentTrainingDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 个人相关
						initPageComboboxStudent();
					}
				});
			} else {
				$.messager.alert('提示', '一次只能修改一条数据！');
			}
		}

		function initPageComboboxStudent() {
			$.ajax({
				url : '${pageContext.request.contextPath}/studentOtherAction!queryStudentOther.action',
				data : {
					studentId : studentId
				},
				dataType : 'json',
				success : function(r) {
					var bloodType = r.bloodType;
					var identityType = r.identityType;
					var politicalStatus = r.politicalStatus;
					console.info('---------------');
					console.info(bloodType);
					console.info(identityType);
					console.info(politicalStatus);
					console.info('---------------');
					$('#student_studentEdit_editStudentOtherForm').form('load', '${pageContext.request.contextPath}/studentOtherAction!queryStudentOther.action?studentId=' + studentId);
					$('#student_studentEdit_editStudentOtherForm input[name=studentId]').val(studentId);
					// 个人相关-血型
					$('#student_studentEdit_bloodTypeCombobox').combobox('reload', '${pageContext.request.contextPath}/codeAction!queryCodeList.action?ctype=' + '14');
					if (bloodType != null && bloodType != "") {
						$('#student_studentEdit_bloodTypeCombobox').combobox('select', bloodType);
					} else {
						$('#student_studentEdit_bloodTypeCombobox').combobox('select', '00');
					}
					// 个人相关-证件类型
					$('#student_studentEdit_identityTypeCombobox').combobox('reload', '${pageContext.request.contextPath}/codeAction!queryCodeList.action?ctype=' + '15');
					if (identityType != null && identityType != "") {
						$('#student_studentEdit_identityTypeCombobox').combobox('select', identityType);
					} else {
						$('#student_studentEdit_identityTypeCombobox').combobox('select', '00');
					}
					// 个人相关-政治面貌
					$('#student_studentEdit_politicalStatusCombobox').combobox('reload', '${pageContext.request.contextPath}/codeAction!queryCodeList.action?ctype=' + '16');
					if (politicalStatus != null && politicalStatus != "") {
						$('#student_studentEdit_politicalStatusCombobox').combobox('select', politicalStatus);
					} else {
						$('#student_studentEdit_politicalStatusCombobox').combobox('select', '00');
					}

				}
			});
		}

		function removeStudent() {
			var rows = studentDatagrid.datagrid('getChecked');
			var ids = [];
			if (rows.length > 0) {
				$.messager.confirm('确认', '确定要删除选择的数据吗？', function(r) {
					if (r) {
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							url : '${pageContext.request.contextPath}/studentAction!removeStudent.action',
							data : {
								ids : ids.join(',')
							},
							dataType : 'json',
							success : function(r) {
								studentDatagrid.datagrid('load');
								studentDatagrid.datagrid('unselectAll');
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！');
			}
		}

		function viewStudent() {
			var rows = studentDatagrid.datagrid('getChecked');
			if (rows.length == 1) {
				studentId = rows[0].id;
				var dlg = $('<div style="padding:10px;" />').dialog({
					width : 900,
					height : 500,
					href : '${pageContext.request.contextPath}/pages/student/studentView.jsp',
					modal : true,
					title : '学生查看',
					onClose : function() {
						$(this).dialog('destroy');
					},
					onLoad : function() {
						// 基本信息
						$('#student_studentEdit_editGeneralForm').form('load', rows[0]);
						// 专业履历
						studentResumeDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 个人爱好
						$('#student_studentEdit_editHobbyForm').form('load', '${pageContext.request.contextPath}/hobbyAction!queryHobby.action?studentId=' + studentId);
						// 车辆信息
						studentVehicleDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 人际关系
						studentRelationshipDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 家庭信息
						studentFamilyDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 教育信息
						studentEducationDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 培训信息
						studentTrainingDatagrid.datagrid('load', {
							studentId : studentId
						});
						// 个人相关
						$('#student_studentEdit_editStudentOtherForm').form('load', '${pageContext.request.contextPath}/studentAction!queryStudentOther.action?id=' + studentId);
					}
				});
			} else {
				$.messager.alert('提示', '一次只能修改一条数据！');
			}
		}

		function importStudent() {
			var dlg = $('<div style="padding:20px 30px;" />').dialog({
				width : 400,
				height : 200,
				href : '${pageContext.request.contextPath}/pages/student/studentImport.jsp',
				modal : true,
				title : '学生导入',
				buttons : [ {
					text : '导入',
					handler : function() {
						$('#student_studentImport_importStudentForm').form('submit', {
							url : '${pageContext.request.contextPath}/studentAction!importStudent.action',
							success : function(r) {
								var obj = jQuery.parseJSON(r);
								if (obj.success) {
									dlg.dialog('close');
									studentDatagrid.datagrid('load');
									studentDatagrid.datagrid('uncheckAll');
									studentDatagrid.datagrid('unselectAll');
									$.messager.show({
										title : '提示',
										msg : obj.msg
									});
								} else {
									$.messager.alert('提示', obj.msg);
								}
							}
						});
					}
				}, {
					text : '取消',
					handler : function() {
						dlg.dialog('close');
					}
				} ],
				onClose : function() {
					$(this).dialog('destroy');
				}
			});
		}

		function downloadTemplate() {
			$.messager.confirm('确认', '确定要下载导入模板吗？', function(r) {
				if (r) {
					window.location.href = '${pageContext.request.contextPath}/studentAction!downloadTemplate.action';
					studentDatagrid.datagrid('uncheckAll');
					studentDatagrid.datagrid('unselectAll');
				}
			});
		}

	});

	function openCorpDialog(page) {
		$('#student_studentList_corpDialog').dialog('open');
		studentCorporationDatagrid = $('#student_studentList_corpDatagrid').datagrid({
			url : '${pageContext.request.contextPath}/corporationAction!queryCorporation.action',
			fit : true,
			border : false,
			pagination : true,
			rownumbers : true,
			striped : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10 ],
			sortName : 'corporationName',
			sortOrder : 'asc',
			checkOnSelect : true,
			selectOnCheck : true,
			singleSelect : true,
			frozenColumns : [ [ {
				field : 'id',
				title : '编号',
				width : 150,
				checkbox : true
			}, {
				field : 'corporationName',
				title : '单位名称',
				width : 150,
				align : 'left',
				sortable : true
			} ] ],
			columns : [ [ {
				field : 'legalPerson',
				title : '法人',
				width : 100,
				align : 'center',
				hidden : true
			}, {
				field : 'contactPerson',
				title : '联系人',
				width : 100,
				align : 'left'
			}, {
				field : 'email',
				title : '电子邮件',
				width : 100,
				align : 'left'
			}, {
				field : 'phone',
				title : '电话',
				width : 100,
				align : 'left'
			}, {
				field : 'fax',
				title : '传真',
				width : 100,
				align : 'left'
			}, {
				field : 'corporationType',
				title : '企业类型',
				width : 100,
				align : 'left'
			}, {
				field : 'region',
				title : '地区',
				width : 200,
				align : 'left'
			}, {
				field : 'industry',
				title : '行业',
				width : 150,
				align : 'left'
			} ] ],
			toolbar : '#student_studentList_corpToolbar',
			onDblClickRow : function(rowIndex, rowData) {
				if ("studentAdd" == page) {
					$('#student_studentAdd_addForm input[name=corporationName]').val(rowData.corporationName);
					$('#student_studentAdd_addForm input[name=corporationId]').val(rowData.id);
				}
				if ("generalAdd" == page) {
					$('#student_studentEdit_editGeneralForm input[name=corporationName]').val(rowData.corporationName);
					$('#student_studentEdit_editGeneralForm input[name=corporationId]').val(rowData.id);
				}
				$('#student_studentList_corpDialog').dialog('close');
			}
		});
		$('#student_studentList_corpDatagrid').datagrid('clearSelections');
	}

	function saveStudent() {
		$('#student_studentEdit_editGeneralForm').form('submit', {
			url : '${pageContext.request.contextPath}/studentAction!editStudent.action',
			success : function(r) {
				var obj = jQuery.parseJSON(r);
				if (obj.success) {
					studentDatagrid.datagrid('updateRow', {
						index : $('#student_studentList_datagrid').datagrid('getRowIndex', studentId),
						row : obj.obj
					});
					$.messager.show({
						title : '提示',
						msg : obj.msg
					});
				} else {
					$.messager.alert('错误', obj.msg);
				}
			},
			onSubmit : function(r) {
				$('#student_studentEdit_editGeneralForm input[name=id]').val(studentId);
			}
		});
	}

	function addOrEditHobby() {
		$('#student_studentEdit_editHobbyForm').form('submit', {
			url : '${pageContext.request.contextPath}/hobbyAction!addOrEditHobby.action',
			success : function(r) {
				var obj = jQuery.parseJSON(r);
				if (obj.success) {
					$.messager.show({
						title : '提示',
						msg : obj.msg
					});
				} else {
					$.messager.alert('提示', obj.msg);
				}
			},
			onSubmit : function(r) {
				$('#student_studentEdit_editHobbyForm input[name=studentId]').val(studentId);
			}
		});
	}

	function addOrEditStudentOther() {
		$('#student_studentEdit_editStudentOtherForm').form('submit', {
			url : '${pageContext.request.contextPath}/studentOtherAction!addOrEditStudentOther.action',
			success : function(r) {
				var obj = jQuery.parseJSON(r);
				if (obj.success) {
					$.messager.show({
						title : '提示',
						msg : obj.msg
					});
				} else {
					$.messager.alert('错误', obj.msg);
				}
			}
		});
	}

	function queryStudentCorporation() {
		studentCorporationDatagrid.datagrid('load', serializeObject($('#student_studentList_queryCorporationForm')));
	}
</script>
<div id="student_studentList_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',border:false">
		<table id="student_studentList_datagrid"></table>
	</div>
</div>
<div id="student_studentList_queryDialog" class="easyui-dialog" data-options="title:'学生查询',closed:true,modal:true,
			buttons:[{
				text : '查询',
				handler : function(){
					$('#student_studentList_datagrid').datagrid('load', serializeObject($('#student_studentList_queryForm')));
					$('#student_studentList_datagrid').datagrid('unselectAll');
					$('#student_studentList_datagrid').datagrid('uncheckAll');
				    $('#student_studentList_queryDialog').dialog('close');
				}
			},{
				text : '清空',
				handler : function(){
					$('#student_studentList_queryForm input').val('');
				}
			}]" style="width:380px;height:220px;padding:10px;" align="center">
	<form id="student_studentList_queryForm" method="post">
		<table>
			<tr>
				<td style="text-align: right;">姓名：</td>
				<td><input name="studentName" style="width:240px;" />
				</td>
			</tr>
			<tr>
				<td style="text-align: right;">学号：</td>
				<td><input name="studentNo" style="width:240px;" />
				</td>
			</tr>
			<tr>
				<td style="text-align: right;">手机：</td>
				<td><input name="mobile" style="width:240px;" />
				</td>
			</tr>
			<tr>
				<td style="text-align: right;">电子邮件：</td>
				<td><input name="email" style="width:240px;" />
				</td>
			</tr>
		</table>
	</form>
</div>
<div id="student_studentList_corpDialog" class="easyui-dialog" data-options="title:'单位查询',closed:true,modal:true" style="width:800px;height:400px;">
	<div id="student_studentList_corpLayout" class="easyui-layout" data-options="fit:true,border:false">
		<div data-options="region:'center',border:false">
			<table id="student_studentList_corpDatagrid"></table>
		</div>
	</div>
</div>
<div id="student_studentList_corpToolbar" style="height:60px;padding:5px 0 0 20px;">
	<form id="student_studentList_queryCorporationForm" method="post">
		<table>
			<tr>
				<td style="text-align: right;">单位名称：</td>
				<td><input name="corporationName" style="width:200px;" /></td>
			</tr>
			<tr>
				<td style="text-align: right;">联系人：</td>
				<td><input name="contactPerson" style="width:200px;" />&nbsp;&nbsp; <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="queryStudentCorporation()">查询</a></td>
			</tr>
		</table>
	</form>
</div>